const express = require('express');
const axios = require('axios')
const router = express.Router();
let client_id = "v3HDY4BmxN9RQ5c8rVyzkABv";
let client_secret = "u5MVmhZn4l5Bu9rHUaHUotUGMGtPLFk8"

// 下载axios   npm i axios 
router.post('/bot', (req, res) => {
    let reqMsg = req.body.msg;
    let config = {
        method: 'post',
        url: `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${client_id}&client_secret=${client_secret}`,
    };
    axios(config)
        .then(function (response) {
            // 请求成功拿到token
            let token = response.data.access_token
            // 发起对话请求
            let data = JSON.stringify({ "messages": [{ "role": "user", "content": reqMsg }] })
            axios({
                method: 'post',
                url: `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=${token}`,
                data: data
            }).then(ress => {
                console.log(ress.data);
                res.send({ data: ress.data })
            }).catch(function (error) {
                res.lose(error)
                console.log(error);
            });

        })
        .catch(function (error) {
            res.lose(error)
            console.log(error);
        });


})

module.exports = router